﻿using Admin.NET.Application.Util.Extension;
using Radius.NET.Interface.Kingdee.Service.Production;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Radius.NET.Interface.Kingdee.Service.StockWareModule.Dto.KindeeStockWareDto;

namespace Radius.NET.Interface.Kingdee.Entity
{
    /// <summary>
    /// 生产领料单
    /// </summary>
    public class PRD_PickMtrl: KingdeePubClass
    {
        private MaterialIssuePlan item;
        private SqlSugarRepository<InventoryEvidenceDetail> _evidenveDetailRespository;
        private Dictionary<string, BD_MATERIAL_Dto> materials;
        private List<STK_Inventory_Dto> jskcList;
        private List<PRD_PPBOM_Dto> pRD_PPBOMs;
        /// <summary>
        /// 
        /// </summary>
        public PRD_PickMtrl() { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="item"></param>
        /// <param name="_evidenveDetailRespository"></param>
        /// <param name="materials"></param>
        /// <param name="jskcList"></param>
        /// <param name="pRD_PPBOMs"></param>
        public PRD_PickMtrl(MaterialIssuePlan item, SqlSugarRepository<InventoryEvidenceDetail> _evidenveDetailRespository, Dictionary<string, BD_MATERIAL_Dto> materials, List<STK_Inventory_Dto> jskcList, List<PRD_PPBOM_Dto> pRD_PPBOMs) 
        {
            this.item = item;
            this._evidenveDetailRespository = _evidenveDetailRespository;
            this.materials = materials;
            this.jskcList = jskcList;
            this.pRD_PPBOMs = pRD_PPBOMs;
        }

        /// <summary>
        /// 实体
        /// </summary>
        public PRD_PickMtrl_Model Model { get; set; }

        public class PRD_PickMtrl_Model
        {
            /// <summary>
            /// 
            /// </summary>
            public int FID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FBillType FBillType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public DateTime? FDate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockOrgId FStockOrgId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockId0 FStockId0 { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FPrdOrgId FPrdOrgId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FWorkShopId FWorkShopId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FOwnerTypeId0 { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FOwnerId0 FOwnerId0 { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FPickerId FPickerId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FSTOCKERID FSTOCKERID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FDescription { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FCurrId FCurrId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FIsCrossTrade { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FVmiBusiness { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FScanBox { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSourceType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FIsOwnerTInclOrg { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public List<FEntityItem> FEntity { get; set; }
        }

        #region 生产领料单明细
        public class FEntityItem
        {
            /// <summary>
            /// 
            /// </summary>
            public int FEntryID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FParentMaterialId FParentMaterialId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FBaseStockActualQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FConsome { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FFPMBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FReserveType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FMaterialId FMaterialId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FEntryWorkShopId FEntryWorkShopId { get; set; }

            public object FAuxPropId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FUnitID FUnitID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FBaseUnitId FBaseUnitId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockUnitId FStockUnitId { get; set; }

            public FSecUnitId FSecUnitId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FAppQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FActualQty { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double? FBaseActualQty { get; set; }

            public double? FSecActualQty { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string FEntryVmiBusiness { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FOptPlanBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockId FStockId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FStockLocId FStockLocId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FLot FLot { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FCobyBomEntryID { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FProcessId FProcessId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FPPBomBillNo { get; set; }

            public string F_asdg_Text { get; set; }


            /// <summary>
            /// 
            /// </summary>
            public string FGroupRow { get; set; }


            public string FExpiryDate { get; set; }



            /// <summary>
            /// 
            /// </summary>
            public int FSrcPickEntryId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSrcBusinessType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FQueryStockUpdate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FProduceDate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FMoBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FMoEntryId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public int FMoId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FPPBomEntryId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FMoEntrySeq { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSrcBillType { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FSrcBillNo { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FEntrySrcInterId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FEntrySrcEnteryId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public double FEntrySrcEntrySeq { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FParentOwnerTypeId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FParentOwnerId FParentOwnerId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string FKeeperTypeId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FKeeperId FKeeperId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public FOwnerId FOwnerId { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public List<FEntity_LinkItem> FEntity_Link { get; set; }
        }
        #endregion

        #region 生成表单数据
        /// <summary>
        /// 生成表单数据
        /// </summary>
        /// <returns></returns>
        public object BuildData()
        {
            PRD_MO_Dto pRD_MO = JsonConvert.DeserializeObject<PRD_MO_Dto>(item.Summary1);
            PRD_PickMtrl obj = new()
            {
                Model = new()
                {
                    FID = 0,
                    FBillNo = item.No.Replace("MO", "MT"),//单据编号
                    FBillType = new()
                    {  //单据类型
                        FNumber = "SCLLD01_SYS"
                    },
                    FDate = item.Date, //日期
                    FStockOrgId = new()
                    {  //发料组织
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FStockId0 = new()
                    {  //仓库
                        FNumber = ""
                    },
                    FPrdOrgId = new()
                    {   //生产组织
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FWorkShopId = new()
                    {   //生产车间
                        FNumber = item.Des_StockID
                    },
                    FOwnerTypeId0 = "BD_OwnerOrg",  //货主类型
                    FOwnerId0 = new()
                    {    //货主
                        FNumber = ""
                    },
                    FPickerId = new()
                    {
                        FNumber = "KD0148"
                    },   //领料人
                    FSTOCKERID = new()
                    {   //仓管员
                        FNAME = ""
                    },
                    FDescription = "",  //备注
                    FCurrId = new()
                    {   //本位币
                        FNumber = ""
                    },
                    FIsCrossTrade = "false",
                    FVmiBusiness = "false",
                    FScanBox = "",
                    FSourceType = "",
                    FIsOwnerTInclOrg = "false",
                    FEntity = new List<PRD_PickMtrl.FEntityItem>()
                }
            };
            foreach (var e in item.FG_MaterialIssueDetailsBatch)
            {
                //var invDetail = item.FG_MaterialIssueDetails.Find(x => x.NOID == e.NOID && x.Inventory_ID == e.Inventory_ID);
                var invDetail = pRD_PPBOMs.Find(x => x.FMaterialID2__FNumber == e.Inventory_ID && x.FEntity_FSEQ == e.NOID.ParseToInt());
                var IqcInvItem = _evidenveDetailRespository.AsQueryable()
                    .LeftJoin<InventoryEvidence>((detail, evi) => detail.Evidence_Number == evi.Evidence_Number)
                    .Where((detail, evi) => evi.Type == 30
                    && detail.Inventory_ID == e.Inventory_ID
                    && detail.ProjectBatch == e.ProjectBatch)
                    .Select((detail, evi) => new InventoryEvidenceDetail()
                    {
                        Evidence_Number = detail.Evidence_Number,
                        Inventory_ID = detail.Inventory_ID,
                        ProjectBatch = detail.ProjectBatch,
                        LogoID = detail.LogoID
                    }).First();

                PRD_PickMtrl.FEntityItem fEntityItem = new()
                {
                    FEntryID = 0,
                    FParentMaterialId = new()
                    {  //产品编码
                        FNumber = item.Inventory_ID
                    },
                    FBaseStockActualQty = e.QTY,  //主库存基本单位实发数量
                    FConsome = "",   //消耗汇总
                    FFPMBillNo = "",// 生产备料单据编号
                    FReserveType = "",  //预留类型
                    FMaterialId = new()
                    {
                        FNumber = e.Inventory_ID  //物料编码
                    },
                    FEntryWorkShopId = new()
                    {
                        FNumber = item.DeptID
                    },
                    FAuxPropId = new
                    {
                        FAUXPROPID__FF100001 = new
                        {
                            FNumber = IqcInvItem == null ? "PP008" : IqcInvItem.LogoID
                        }
                    },
                    FUnitID = new()
                    {   //单位 需要赋值
                        FNumber = e.FG_Inventory is not null ? e.FG_Inventory.Unit : "kg"
                    },
                    FBaseUnitId = new()
                    {   //基本单位 需要赋值
                        FNumber = e.FG_Inventory is not null ? e.FG_Inventory.Unit : "kg"
                    },
                    FStockUnitId = new()
                    {   //主库存单位 需要赋值
                        FNumber = e.FG_Inventory is not null ? e.FG_Inventory.Unit : "kg"
                    },
                    FSecUnitId = new()
                    {
                        FNumber = materials.ContainsKey(e.Inventory_ID) ? materials[e.Inventory_ID].FAuxUnitID__FNumber : "Tong"
                    },
                    FAppQty = e.QTY,  //申请数量
                    FActualQty = e.QTY, //实发数量
                    FBaseActualQty = e.QTY, //基本单位实发数量
                    //FSecActualQty = e.QTY,
                    FEntryVmiBusiness = "false",
                    FOptPlanBillNo = "",  //工序计划单编号
                    FStockId = new()
                    {
                        FNumber = item.Des_StockID  //仓库
                    },
                    FStockLocId = new()
                    {
                        FNumber = ""  //仓位
                    },
                    FLot = new()
                    {
                        FNumber = e.ProjectBatch
                    },
                    FCobyBomEntryID = 0,
                    FProcessId = new()
                    {  //作业
                        FNumber = ""
                    },
                    F_asdg_Text = item.ProjectBatch,
                    FPPBomBillNo = invDetail == null ? "0" : invDetail.FBillNo,  //投料单的单据编号
                    FGroupRow = "",
                    FSrcPickEntryId = 0,
                    FSrcBusinessType = "",   //业务来源类型
                    FQueryStockUpdate = "",
                    FProduceDate = e.MakeBeginDate?.ToString("yyyy-MM-dd"),  //生产日期
                    FExpiryDate = e.MEndDate?.ToString("yyyy-MM-dd"),
                    FMoBillNo = item.No.Split("_")[0],  //生产订单编号  工程单下划线前面的
                    FMoEntryId = pRD_MO.FTreeEntity_FEntryId,     //生产订单的分录ID
                    FMoId = pRD_MO.FID,//生产订单的主键ID
                    FPPBomEntryId = invDetail == null ? "0" : invDetail.FEntity_FEntryID,   //投料单的分录ID
                    FMoEntrySeq = item.No.Split("_")[1],        //生产订单的分录行号  工程单下划线后面的
                    FSrcBillType = "PRD_PPBOM",  //原单单据类型  写死的
                    FSrcBillNo = invDetail == null ? "0" : invDetail.FBillNo,  //原单单据编号  投料单的单据单号
                    FEntrySrcInterId = invDetail == null ? "0" : invDetail.FID,    //投料单的主键ID
                    FEntrySrcEnteryId = invDetail == null ? "0" : invDetail.FEntity_FEntryID,    //投料单的分录ID
                    FEntrySrcEntrySeq = invDetail == null ? 0 : invDetail.FEntity_FSEQ,        //投料单的分录行号
                    FParentOwnerTypeId = "BD_OwnerOrg",
                    FParentOwnerId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FKeeperTypeId = "BD_KeeperOrg",
                    FKeeperId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FOwnerId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FEntity_Link = new List<Entity.FEntity_LinkItem>() {
                       new () {
                            FEntity_Link_FFlowId= "81119477-4778-4d0b-94b9-1c43a1c1f768",
                            FEntity_Link_FFlowLineId= 5,
                            FEntity_Link_FRuleId= "PRD_PPBOM2PICKMTRL_NORMAL",
                            FEntity_Link_FSTableName= "T_PRD_PPBOMENTRY",
                            FEntity_Link_FSBillId= invDetail == null ? "0" :invDetail.FID, //投料单的主键ID
                            FEntity_Link_FSId= invDetail == null ? "0" : invDetail.FEntity_FEntryID, //投料单的分录ID
                            FEntity_Link_FBaseActualQtyOld= e.QTY,//原始携带量   ---生产领料单的实发数量
                            FEntity_Link_FBaseActualQty= e.QTY //修改携带量   ---生产领料单的实发数量
                       }
                    }
                };
                obj.Model.FEntity.Add(fEntityItem);
            }
            foreach (var pack in item.FG_MaterialIssuePack)
            {
                //根据物料编码、库位、批次号查询云星空即时库存记录
                var jskcItem = jskcList.Find(x => x.FMaterialId__FNumber == pack.JarModelID && x.FStockId__FNumber == item.Des_StockID && x.FLot__FNumber == pack.CF01);
                var invDetail = pRD_PPBOMs.Find(x => x.FMaterialID2__FNumber.Equals(pack.JarModelID));
                PRD_PickMtrl.FEntityItem fEntityItem = new()
                {
                    FEntryID = 0,
                    FParentMaterialId = new()
                    {  //产品编码
                        FNumber = item.Inventory_ID
                    },
                    FBaseStockActualQty = pack.ActualBoxQty,  //主库存基本单位实发数量
                    FConsome = "",   //消耗汇总
                    FFPMBillNo = "",// 生产备料单据编号
                    FReserveType = "",  //预留类型
                    FMaterialId = new()
                    {
                        FNumber = pack.JarModelID  //物料编码
                    },
                    FEntryWorkShopId = new()
                    {
                        FNumber = item.DeptID
                    },
                    FAuxPropId = new
                    {
                        FAUXPROPID__FF100001 = new
                        {
                            FNumber = jskcItem is null ? "" : jskcItem.FAuxPropId__FF100001__FNumber
                        }
                    },
                    FUnitID = new()
                    {   //单位 需要赋值
                        FNumber = materials.ContainsKey(pack.JarModelID) ? materials[pack.JarModelID].FBaseUnitId__FNumber : "Tong"
                    },
                    FBaseUnitId = new()
                    {   //基本单位 需要赋值
                        FNumber = materials.ContainsKey(pack.JarModelID) ? materials[pack.JarModelID].FBaseUnitId__FNumber : "Tong"
                    },
                    FStockUnitId = new()
                    {   //主库存单位 需要赋值
                        FNumber = materials.ContainsKey(pack.JarModelID) ? materials[pack.JarModelID].FStoreUnitID__FNumber : "Tong"
                    },
                    FSecUnitId = new()
                    {
                        FNumber = materials.ContainsKey(pack.JarModelID) ? materials[pack.JarModelID].FAuxUnitID__FNumber : "Tong"
                    },
                    FAppQty = pack.ActualBoxQty,  //申请数量
                    FActualQty = pack.ActualBoxQty, //实发数量
                    FBaseActualQty = pack.ActualBoxQty, //基本单位实发数量
                    //FSecActualQty = pack.ActualBoxQty,
                    FEntryVmiBusiness = "false",
                    FOptPlanBillNo = "",  //工序计划单编号
                    FStockId = new()
                    {
                        FNumber = item.Des_StockID  //仓库
                    },
                    FStockLocId = new()
                    {
                        FNumber = ""  //仓位
                    },
                    FLot = new()
                    {
                        FNumber = pack.CF01  //批次号
                    },
                    FCobyBomEntryID = 0,
                    FProcessId = new()
                    {
                        FNumber = ""  //作业
                    },
                    F_asdg_Text = item.ProjectBatch,
                    FPPBomBillNo = invDetail == null ? "0" : invDetail.FBillNo,  //投料单的单据编号
                    FGroupRow = "",
                    FSrcPickEntryId = 0,
                    FSrcBusinessType = "",   //业务来源类型
                    FQueryStockUpdate = "",
                    FProduceDate = item.CF02,//e.MakeBeginDate?.ToString("yyyy-MM-dd"),  //生产日期
                    FExpiryDate = item.CF03,//e.MEndDate?.ToString("yyyy-MM-dd"),
                    FMoBillNo = item.No.Split("_")[0],  //生产订单编号  工程单下划线前面的
                    FMoEntryId = pRD_MO.FTreeEntity_FEntryId,     //生产订单的分录ID
                    FMoId = pRD_MO.FID,//生产订单的主键ID
                    FPPBomEntryId = invDetail == null ? "0" : invDetail.FEntity_FEntryID,   //投料单的分录ID
                    FMoEntrySeq = item.No.Split("_")[1],        //生产订单的分录行号  工程单下划线后面的
                    FSrcBillType = "PRD_PPBOM",  //原单单据类型
                    FSrcBillNo = invDetail == null ? "0" : invDetail.FBillNo,  //原单单据编号  投料单的单据单号
                    FEntrySrcInterId = invDetail == null ? "0" : invDetail.FID,    //投料单的主键ID
                    FEntrySrcEnteryId = invDetail == null ? "0" : invDetail.FEntity_FEntryID,    //投料单的分录ID
                    FEntrySrcEntrySeq = invDetail == null ? 0 : invDetail.FEntity_FSEQ,        //投料单的分录行号
                    FParentOwnerTypeId = "BD_OwnerOrg",
                    FParentOwnerId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FKeeperTypeId = "BD_KeeperOrg",
                    FKeeperId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FOwnerId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FEntity_Link = new List<Entity.FEntity_LinkItem>() {
                       new () {
                            FEntity_Link_FFlowId = "81119477-4778-4d0b-94b9-1c43a1c1f768",
                            FEntity_Link_FFlowLineId = 5,
                            FEntity_Link_FRuleId = "PRD_PPBOM2PICKMTRL_NORMAL",
                            FEntity_Link_FSTableName = "T_PRD_PPBOMENTRY",
                            FEntity_Link_FSBillId= invDetail == null ? "0" :invDetail.FID,   //投料单的主键ID
                            FEntity_Link_FSId= invDetail == null ? "0" : invDetail.FEntity_FEntryID, //投料单的分录ID
                            FEntity_Link_FBaseActualQtyOld = pack.ActualBoxQty, //原始携带量   ---生产领料单的实发数量
                            FEntity_Link_FBaseActualQty = pack.ActualBoxQty //修改携带量   ---生产领料单的实发数量
                       }
                    }
                };

                obj.Model.FEntity.Add(fEntityItem);
            }
            return obj;
        } 
        #endregion
    }
}
